/*
 * @Descripttion:
 * @Author: voanit
 * @Date: 2022-05-27 16:15:45
 * @LastEditors: voanit
 * @LastEditTime: 2022-06-04 16:36:01
 */
const express = require('express')
const app = express()
const http = require('http').createServer(app)
const io = require('socket.io')(http, { cors: true })

io.on('connection', socket => { // 客户端链接成功
  console.log('~connection~')
  io.emit('message', { msg: '我是服务器' })
  // setInterval(()=>{
  //     io.emit("message", "我是服务器：" + Date.now());
  // }, 1000)
  socket.on('message', msg => { // 监听的频道必须和客户端监听的频道相同，等待消息
    io.emit('message', msg) // 向所有客户端发送信息
  })

  io.on('disconnect', _ => { // 客户端断开链接

  })
})

http.listen(3009, _ => {
  console.log('http://127.0.0.1:3009')
})
